<template>
  <div class="container with-nav-padding">
    <div v-if="!showProtocol">
      <div class="balance_wrap">
        <p class="account_balance">账户余额（元）</p>
        <p class="balance_num">{{ balance_num }}</p>
        <p @click="$router.push('/balance-detail')" class="query_balance">
          查看明细<img src="@/assets/车主宝首页改-assets/返回.png" alt="" />
        </p>
        <i @click="showProtocol = true"
          ><img class="" src="@/assets/余额-assets/提示1.png" alt=""
        /></i>
      </div>
      <div class="withdrawCash with-grey-bottom-border">
        <div class="withdrawCashTit">
          <p>输入提现金额（元）</p>
        </div>
        <form>
          <span class="form_wrap">
            <i>￥</i>
            <input type="text" :placeholder="tips()" v-model="withdraw" />
            <p></p>
          </span>
        </form>
      </div>
      <div class="tips">
        最低提现金额100元，手续费3%
      </div>
      <div @click="drawMoney" class="withdrawCashBtn" :style="{background: mainColor}">提现</div>
    </div>
    <div v-else class="protocol">
      提现规则:
      <br />
      最低提现金额100元 提现手续费3%
    </div>
  </div>
</template>

<script>
import { getBalanceDetail, drawMoney } from "@/api/vip";
import { getUserInfo } from "@/api/profile";
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      balance_num: 0,
      protocol: "",
      withdraw: null,
      showProtocol: false
    };
  },
  computed: {
    ...mapState({
      is_identity: state => state.user.is_identity,
      mainColor: state => state.app.mainColor
    })
  },
  methods: {
    ...mapActions(["setUserInfo"]),
    tips: function() {
      let tiptext = "提现金额，最多提现" + this.balance_num;
      return tiptext;
    },
    drawMoney() {
      if (this.withdraw - 100 < 0) {
        this.$dialog.toast({
          mes: "最低提现金额为100元"
        });
        return;
      }
      if (this.is_identity === 1) {
        drawMoney({ money: this.withdraw }).then(res => {
          if (res.status === 200) {
            this.$dialog.toast({
              mes: "提现成功，请耐心等待",
              timeout: 2000,
              icon: "success"
            });
            this.withdraw = null;
            this.getBalanceDetail();
          }
        });
      } else {
        this.$dialog.toast({
          mes: "请认证后再提现",
          timeout: 1500
        });
        this.$router.push("/real-name-auth");
      }
    },
    getBalanceDetail() {
      getBalanceDetail().then(res => {
        if (res.status === 200) {
          this.balance_num = res.data.data.balance;
          this.protocol = res.data.data.draw_money;
        }
      });
    }
  },
  created() {
    this.getBalanceDetail();
    if (this.is_identity === null) {
      getUserInfo().then(res => {
        if (res.status === 200) {
          this.setUserInfo(res.data.data);
        }
      });
    }
  },
  beforeRouteLeave(to, from, next) {
    if (this.showProtocol === true) {
      this.showProtocol = false;
      next(false);
    } else {
      next();
    }
  }
};
</script>

<style lang="less" scoped>
.container {
  background: #fff;
  min-height: 100%;
  height: auto;
  .clear {
    clear: both;
  }
  .balance_wrap {
    padding: 1.46rem 0.5rem;
    background: #ffffff;
    width: 100%;
    text-align: center;
    position: relative;
    margin-bottom: 0.4rem;
    border-top: 1px solid #e9e9e9;
    border-bottom: 1px solid #e9e9e9;
    p {
      margin-bottom: 0.35rem;
    }
    .account_balance {
      color: #999999;
      font-size: 0.56rem;
    }
    .balance_num {
      color: #444444;
      font-size: 1.2rem;
      font-weight: bold;
    }
    .query_balance {
      color: #444444;
      font-size: 0.48rem;
      img {
        width: 0.22rem;
        margin-left: 0.2rem;
      }
    }
    i img {
      width: 0.82rem;
      position: absolute;
      right: 0.88rem;
      top: 0.4rem;
    }
  }
  .withdrawCash {
    background: #ffffff;
    padding-top: 0.5rem;

    .withdrawCashTit {
      padding-left: 0.48rem;
      p {
        border-left: 0.12rem solid #0078ff;
        padding-left: 0.56rem;
        font-size: 0.64rem;
        margin-bottom: 0.3rem;
        color: #444444;
      }
    }
    .form_wrap {
      display: block;
      margin-top: 1.42rem;
      padding-left: 1.06rem;
      i {
        font-size: 0.48rem;
        color: #444444;
      }
      input::-webkit-input-placeholder {
        /* WebKit browsers */
        color: #999999;
        font-size: 0.48rem;
      }
      input::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: #999999;
        font-size: 0.48rem;
      }
      input {
        width: 8rem;
        height: 1.2rem;
        padding-left: 0.2rem;
        font-size: 0.48rem;
        color: #444444;
        border: none;
      }
    }
  }
  .tips {
    margin-top: 0.48rem;
    padding-left: 1rem;
    font-size: 0.44rem;
    color: red;
  }
  .withdrawCashBtn {
    width: 14.04rem;
    height: 2rem;
    line-height: 2rem;
    text-align: center;
    font-size: 0.56rem;
    color: #ffffff;
    background: #0078ff;
    margin: 4.6rem auto 0;
  }
  .protocol {
    font-size: 0.56rem;
    padding: 0.48rem 3.2%;
  }
}
</style>
